package com.app.couponsharelife.ui.main
import android.content.Intent
import cn.jpush.android.api.JPushInterface
import com.app.couponsharelife.R
import com.app.couponsharelife.base.BaseActivity
import com.app.couponsharelife.ui.main.login.NewLoginActivity
import com.app.couponsharelife.utils.AppUtils
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import org.json.JSONObject
import java.lang.Exception
import java.util.concurrent.TimeUnit

class SplashActivity : BaseActivity() {
    override val viewLayoutId: Int get() = R.layout.activity_splash
    private var disposable: Disposable? = null

    override fun initViews() {
        AppUtils.initImmersionBar(this,true)
        /*val bundle = this.intent.extras
        if (bundle!=null){
            val type=bundle.get("Type")?:""
            Log.e("eeee","SplashActivity---->$type")
        }*/
    }

    override fun allPermAllow() {
        if (disposable == null)
            disposable = Observable.interval(0, 1, TimeUnit.SECONDS)//10s 倒计时
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe({ aLong -> if (aLong != 0L) goAhead() }) { goAhead() }
    }

    private fun goAhead() {
        if (AppUtils.getString("is_first", "") == "1") {
            if (AppUtils.checkUserLogin()){
                val bundle=intent.extras
                val intents=Intent(this, MainActivity::class.java)
                if (bundle!=null){
                    val extras = bundle.getString(JPushInterface.EXTRA_EXTRA)?:""
                    if (extras.isNotEmpty()){
                        try {
                            val json=JSONObject(extras)
                            val type=json.opt("type") as String
                            val link=json.opt("datas") as String
                            //Log.e("eeee","SplashActivity------>$type---->$link")
                            intents.putExtra("type",type)
                            intents.putExtra("datas",link)
                        }catch (e:Exception){

                        }
                    }
                }
                startActivity(intents)
            }else{
                startActivity(Intent(this, NewLoginActivity::class.java))
            }
        } else {
            startActivity(Intent(this, WelcomeActivity::class.java))
        }
        disposable?.dispose()
        disposable == null
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        AppUtils.destroyImmersionBar(this)
        disposable?.dispose()
        disposable = null
        AppUtils.destroyImmersionBar(this)
    }
}
